#include "widget.h"
#include <QDebug>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    pushbutton = new QPushButton(this);

    labelX = new QLabel(this);
    Xvalue = new QLabel(this);

    labelY = new QLabel(this);
    Yvalue = new QLabel(this);

    labelZ = new QLabel(this);
    Zvalue = new QLabel(this);

    layout = new QGridLayout(this);

    //labelX->show();
    labelX->setText("X");

    //labelY->show();
    labelY->setText("Y");

    //labelZ->show();
    labelZ->setText("Z");

    //pushbutton->show();
    pushbutton->setText("Start");

    //QGridLayout
    layout->addWidget(labelX, 0, 0, 1, 1);
    layout->addWidget(Xvalue, 0, 1, 1, 1);

    layout->addWidget(labelY, 1, 0, 1, 1);
    layout->addWidget(Yvalue, 1, 1, 1, 1);

    layout->addWidget(labelZ, 2, 0, 1, 1);
    layout->addWidget(Zvalue, 2, 1, 1, 1);

    layout->addWidget(pushbutton, 3, 0, 1, 2);

    layout->setSpacing(20);
    layout->setContentsMargins( 90, 90, 100, 100);

    accelerometer = new QAccelerometer(this);

    connect( accelerometer, SIGNAL(readingChanged()), this, SLOT(MyShow()));
    connect( pushbutton, SIGNAL(clicked()), this, SLOT(Switch()));
}

Widget::~Widget()
{

}

void Widget::Switch()
{
    if (accelerometer->isActive())
    {
        accelerometer->stop();
        pushbutton->setText("Start");
    }
    else
    {
        accelerometer->start();
        pushbutton->setText("Stop");
    }
}

void Widget::MyShow()
{

    QString x = QString::number(accelerometer->reading()->x());
    QString y = QString::number(accelerometer->reading()->y());
    QString z = QString::number(accelerometer->reading()->z());

    this->Xvalue->setText(x);
    this->Yvalue->setText(y);
    this->Zvalue->setText(z);

}
